package com.xsherl.leetcode.solution;

import com.xsherl.leetcode.base.TreeNode;
import com.xsherl.leetcode.utils.PrintUtils;

public class ConvertSortedArrayToBinarySearchTree {

    public TreeNode sortedArrayToBST(int[] nums) {
        return buildTree(nums, 0, nums.length - 1);
    }

    public TreeNode buildTree(int[] nums, int begin, int end){
        if (begin > end){
            return null;
        }
        int mid = (begin + end) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = buildTree(nums, begin, mid - 1);
        root.right = buildTree(nums, mid + 1, end);
        return root;
    }

    public static void main(String[] args) {
        int[] nums = {-10,-3, -2, -1,0,5,9};
        TreeNode node = new ConvertSortedArrayToBinarySearchTree().sortedArrayToBST(nums);
        PrintUtils.println(node);
    }
}
